package com.tianji.remark.mq;

import com.tianji.api.dto.remark.LikedRecordsDTO;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.service.ILikedRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.QA_LIKED_RECORDS_KEY;

@Component
@RequiredArgsConstructor
@Slf4j
public class LikedRecordMqListener {

    private final ILikedRecordService likedRecordService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.liked.records.queue", durable = "true"),
            exchange = @Exchange(name = LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = QA_LIKED_RECORDS_KEY
    ))
    public void listenReplyLikedRecordsChange(List<LikedRecordsDTO> likedRecords) {
        log.debug("监听到点赞记录变更，数量：{}", likedRecords.size());
        if (CollUtils.isEmpty(likedRecords)) {
            return;
        }
        try {
            // 分离点赞和取消点赞记录
            List<LikedRecord> recordsToAdd = new ArrayList<>();
            List<LikedRecord> recordsToRemove = new ArrayList<>();

            for (LikedRecordsDTO dto : likedRecords) {
                if (Boolean.TRUE.equals(dto.getLiked())) {
                    // 点赞记录：转换为实体并添加到插入列表
                    LikedRecord record = new LikedRecord();
                    BeanUtils.copyProperties(dto, record);
                    recordsToAdd.add(record);
                } else {
                    // 取消点赞记录：添加到删除列表
                    LikedRecord record = new LikedRecord();
                    BeanUtils.copyProperties(dto, record);
                    recordsToRemove.add(record);
                }
            }
            log.debug("点赞记录 - 新增: {} 条, 删除: {} 条", recordsToAdd.size(), recordsToRemove.size());
            // 批量处理新增记录
            if (CollUtils.isNotEmpty(recordsToAdd)) {
                boolean success = likedRecordService.saveBatch(recordsToAdd);
                if (success) {
                    log.info("remark服务批量新增点赞记录: {} 条, 结果: {}", recordsToAdd.size(), true);
                } else {
                    log.error("新增点赞记录失败");
                }
            }
            // 批量处理删除记录
            if (CollUtils.isNotEmpty(recordsToRemove)) {
                int deleteCount = likedRecordService.batchDeleteByUserIdAndBizId(recordsToRemove);
                log.info("remark服务批量删除点赞记录: {} 条", deleteCount);
            }
            log.debug("点赞记录处理完成");
        } catch (Exception e) {
            log.error("处理点赞记录变更失败", e);
            // 可以根据需要添加重试逻辑
        }
    }
}